package com.sangmo.fts.api.inventory;

import com.sangmo.boot.framework.api.utils.ApiResponse;
import com.sangmo.boot.framework.common.security.meta.Access;
import com.sangmo.fts.basic.model.FileUpload;
import com.sangmo.fts.process.inventory.interfaces.IfContractDocumentRequest;
import com.sangmo.fts.process.inventory.service.IfContractDocumentService;
import com.sangmo.fts.restructure.meta.TrainingApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

@TrainingApi
@Controller
@RequestMapping(value = "/ifContract/v1")
@Access("ifContract")
public class IfContractDocumentApi {

    @Autowired
    private IfContractDocumentService ifContractDocumentService;

    @Access(":sellerUpload")
    @PostMapping({"/{trainingId}/{id}/pledgor/document/upload"})
    @ResponseBody
    public ApiResponse<FileUpload> createPledgorFile(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id, @RequestBody @Valid IfContractDocumentRequest request) {
        return ApiResponse.make(ifContractDocumentService.createPledgorDoc(id, request.getFileUploadId()));
    }

    @Access(":bankUpload")
    @PostMapping({"/{trainingId}/{id}/pledgee/document/upload"})
    @ResponseBody
    public ApiResponse<FileUpload> createPledgeeFile(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id, @RequestBody @Valid IfContractDocumentRequest request) {
        return ApiResponse.make(ifContractDocumentService.createPledgeeDoc(id, request.getFileUploadId()));
    }

    @Access(":storageUpload")
    @PostMapping({"/{trainingId}/{id}/regulator/document/upload"})
    @ResponseBody
    public ApiResponse<FileUpload> createRegulatorFile(@PathVariable("trainingId") Long trainingId, @PathVariable("id") Long id, @RequestBody @Valid IfContractDocumentRequest request) {
        return ApiResponse.make(ifContractDocumentService.createRegulatorDoc(id, request.getFileUploadId()));
    }
}
